<?php
namespace app\member\controller;

use app\common\controller\BaseMember;
use app\common\model\User;
use app\common\model\Archive;
use app\common\model\ArchiveReply;
use app\common\model\ZanLog;
use app\common\model\Arctype;

class Index extends BaseMember {
    public function _initialize(){
        parent::_initialize();

    }

    public function index() {
		$Archive = new Archive();
		$arcRep = new ArchiveReply();
		$zanLog = new ZanLog();
		$arcNum = $Archive->where( ['writer'=>$this->uid,'status'=>1] )->count();
		$arcRepNum = $arcRep->where( ['uid'=>$this->uid,'audit'=>1] )->count();
		$zanLogNum = $zanLog->where( ['uid'=>$this->uid,'a_id'=>['neq','']] )->count();

		$this->assign('arcNum',$arcNum);//文章数量
		$this->assign('arcRepNum',$arcRepNum);//回复数量
		$this->assign('zanLogNum',$zanLogNum);//赞数量
        return $this->fetch();

    }

    public function archive($type) {	// 发布/回复/赞的文章列表
		$Archive = new Archive();
		if( $type == 'myreply' ){
			$title = '回复的文章';
			$arcRep = new ArchiveReply();
			$arcRepNum = $arcRep->where( ['uid'=>$this->uid,'audit'=>1] )->select();
			$repIdArr = [];
			foreach ($arcRepNum as $k => $v) {
				$repIdArr[] = $v['aid'];
			}
			$arcLists = $Archive->where( ['status'=>1,'id'=>['in',$repIdArr]] )->order('id desc')->paginate(10);
		}elseif( $type == 'myzan' ){
			$title = '赞的文章';
			$zanLog = new ZanLog();
			$zanLogNum = $zanLog->where( ['uid'=>$this->uid,'a_id'=>['neq','']] )->select();
			$repIdArr = [];
			foreach ($zanLogNum as $k => $v) {
				$repIdArr[] = $v['a_id'];
			}
			$arcLists = $Archive->where( ['status'=>1,'id'=>['in',$repIdArr]] )->order('id desc')->paginate(10);
		}else{
			$title = '发布的文章';
			$arcLists = $Archive->where( ['writer'=>$this->uid,'status'=>1] )->order('id desc')->paginate(10);
		}
		foreach ($arcLists as $k => $v) {
			$arcLists[$k]['arctypeurl'] = url('@category/'.$v->arctype->dirs);   //文章栏目链接
			$arcLists[$k]['arcurl'] = url('@detail/'.$v->arctype->dirs.'/'.$v['id']);   //文章链接
			if( empty($v['litpic']) ){
            	$imgarr = getImgs($v['addonarticle']['content']);
            	$v['litpic'] = $imgarr[0];
            }
		}
		$this->assign('arcLists',$arcLists);
		$this->assign('title',$title);
		$this->assign('empty', '<div class="media box box-solid x-p-10"><div class="media-body text-center">暂时没有数据</div></div>');   //没有数据模版
        return $this->fetch();
    }

    public function edit($id) {	//编辑文章
		$Archive = new Archive();
		$arcData= $Archive->where( ['id'=>$id] )->find();
		if( !empty($arcData) ){
			if( $arcData['writer'] != $this->uid ){
				$this->error('不是您发布的文章，不能编辑。');
			}
		}
		if(request()->isPost()){
			$data = input('post.');
			$data['status'] = confv('is_addarticle_audit','system');
			$result = $Archive->validate('Archive.add')->allowField(true)->save($data,$data['id']);
            $mod = $data['mod'];
            $addonData = db($mod)->field('id', true)->strict(false)->where( 'aid='.$data['id'] )->update($data);   //关联表数据
            if ($result && $addonData){
            	if(confv('is_addarticle_audit','system') == 0){
            		$info = '编辑成功，需要审核后才能显示。';
            	}else{
            		$info = '编辑成功';
            	}
                return ajaxReturn($info, url('/userinfo/'.$this->uid));
            }else{
                return ajaxReturn($Archive->getError());
            }
		}else{
			$arctype = new Arctype();
			$arclist = $arctype->where(['status'=>1,'is_release'=>1])->order('sorts ASC,id ASC')->select();

			$this->assign('arclist',$arclist);
			$this->assign('data',$arcData);
			$this->assign('titles','编辑');
	        return $this->fetch('index@detail/addArticle');
		}


    }

    public function delete() {	//删除文章
        if (request()->isPost()){
        	$Archive = new Archive();
            $id = input('id');
			$arcData= $Archive->where( ['id'=>$id] )->find();
			if( !empty($arcData) ){
				if( $arcData['writer'] != $this->uid ){
					return ajaxReturn('不是您发布的文章，不能删除。');
				}
			}
            if (isset($id) && !empty($id)){
                $id_arr = explode(',', $id);
                if (!empty($id_arr)){
					$arc_reply = new \app\common\model\ArchiveReply;
                    foreach ($id_arr as $val){
                        $addonMod = $Archive->where(['id' => $val])->value('mod');
                    	$imgurl = $Archive->where(['id' => $val])->value('litpic');
						if(!empty($imgurl)){
                    		unlink(WEB_PATH.$imgurl);	//删除图片
						}
                        $Archive->where('id='.$val)->delete();
                        db($addonMod)->where('aid='.$val)->delete();   //关联表数据
                        $arc_reply_id = $arc_reply->where(['aid'=>$val])->column('id');
						$where = ['in',$arc_reply_id];
						db('ZanLog')->where(['ar_id'=>$where])->delete();   //删除 文章回复 赞数据
						db('ZanLog')->where(['a_id'=>$val])->delete();   //删除 文章 赞数据
						$arc_reply->where(['aid'=>$val])->delete();	//删除 文章回复
                    }
                    return ajaxReturn('删除成功', url('/userinfo/'.$this->uid));
                }else{
                    return ajaxReturn('删除失败');
                }
            }
        }

    }

    public function abc() {

//		$this->error('操作失败');
		$this->success('成功啦');

        return $this->fetch();
    }

    public function aaa() {

//		$this->error('操作失败');
//		$this->success('成功啦');

        return $this->fetch();
    }

    public function bbs() {

		$dd = '';
		$gl = getImgs($dd);

		$this->assign('dd',$gl);

//		p($gl);
        return $this->fetch();
    }

}
